<template>
	<view class="iPage fixed full flex">
		<view class="relt" style="width:476rpx;" v-if="activity=='activeA'">
			<view class="outer">
				<view class="inner" :style='{width:progress}'></view>
			</view>
			<view class="outer outnone">
				<view class="inner" :style='{width:progress}'>
					<image class="earth earth1" mode="scaleToFill" src="/static/walk.png"></image>
				</view>
			</view>
			<image class="loadbg" style="height:146rpx;" src="/static/load-1.png" mode="scaleToFill"></image>
		</view>
		<view class="relt" style="width:602rpx;" v-if="activity=='activeB'&&!noload">
			<view class="outer out2">
				<view class="inner" :style='{width:progress}'></view>
			</view>
			<view class="outer out2 outnone">
				<view class="inner" :style='{width:progress}'>
					<image class="earth" mode="widthFix" src="/static/load-3.png"></image>
				</view>
			</view>
			<image class="loadbg" style="height:146rpx;" src="/static/load-2.png" mode="scaleToFill"></image>
		</view>
		<NickHead v-show="userBool" @success="finishBtn"></NickHead>
	</view>
</template>
<script>

	import utils from '@/src/utils.js'
	import user from '@/src/data/user.js'
	import publicData from '@/src/data/publicData'
	import http from '@/src/http.js'
	import platform from "@/src/platform";
	import {resourceA,resourceB} from '@/src/data/activeSource.js'
	import NickHead from '@/components/nick-head.vue'

	export default {
		components:{
			NickHead
		},
		data() {
			return {
				progress:'0%',
				limitBool:false,//是否有活动权限
				activity:'', //活动名称activeA，activeB
				mbtiStatus:'',
				noload:false,
				userBool:false
			}
		},
		onLoad(options) {
			if(options.scene) {
				options.name = 'activeB'
			}
			this.options = options;
            console.info('loading接收参数options：',options)
		},
		async onShow() {
			const systemInfo = wx.getSystemInfoSync();
			const options = this.options;
			if(systemInfo.environment=='wxwork'){
				this.noPermission(3)
				return false;
			}

			/* #ifdef MP */
			await platform.getUserInfo(); //加载用户信息，所有请求都应该在这个之后；
			/* #endif */
			/**********************************************/

			this.toPage = decodeURIComponent(options.page||'');//如果要跳转，跳转的页面参数/pages/index?page=enCode(/pages/activeB/index/index?a=1)
			console.info('toPage参数:',this.toPage)

			if(this.toPage&&this.toPage.includes('activeA')){
				this.activity = 'activeA'
			}else if(this.toPage&&this.toPage.includes('activeB')){
				this.activity = 'activeB'
			}else{
				this.activity = options.name||'activeA';//活动类型:/pages/index?activeA，activeB
			}
			if(!this.activity){
				return console.error('未知的活动，请确认活动类型：activeA 或者 activeB')
			}
			wx.activity = this.activity;
			if(user.identity!=1){ //非本次活动的职工，拦截无权限
				this.noPermission(user.identity);
				return false;
			}
			/**********************************************************/
			if(!user.avatar||!user.nickname){   //如果没有完成头像昵称验证
				this.userBool = true;
			}else{
				this.userInfoSucc();
			}
		},
		methods: {
			userInfoSucc(){
				const options = this.options;
				const noload = options.noload||false;
				this.noload = noload;
				/***********************************/

				// if(user.nickname.includes('~~~')){
				// 	return false;
				// }
				/****************************************************/



				/******************拼接上带域名的url前缀*************************/
				let imgList = (this.activity=='activeA'?resourceA:resourceB).map(img=>{
					return this.imgPath + img;
				});
				if(!noload){
					utils.preloadImg(imgList,(progress)=>{
						this.progress = progress + '%';
						if(progress>99){
							this.loadSucc();
						}
					});
				}else{
					this.loadSucc();
				}
				if(wx.bgMusic&&!noload){
					wx.bgMusic.stop&&wx.bgMusic.stop();
					wx.bgMusic.destory&&wx.bgMusic.destory()
				}
				wx.activity = this.activity;
				if(this.activity=='activeA'){
					wx.bgMusic = platform.getAudioPlayer(this.imgPath+'/static/big/activeA.mp3');
					wx.musicStatus = 'play';
					http.writeLog({type:1});
				}
				if(this.activity=='activeB'&&!noload){
					wx.bgMusic = platform.getAudioPlayer(this.imgPath+'/static/big/activeB.mp3');
				    wx.musicStatus = 'play';
					http.writeLog2({type:1});
				}
			},
			finishBtn(){
				this.userBool = false;
				this.userInfoSucc();
			},
		    async loadSucc(){
		 			await this.loadGlobalData()//加载初始化数据
					if(this.activity=='activeB'){
						await this.loadData();
						if(!this.toPage&&!this.mbtiStatus){
							uni.redirectTo({
								url:'/pages/activeB/index/index'
							})
							return;
						}else if(!this.toPage&&(this.mbtiStatus == 1||(this.mbtiStatus == 2&&this.teamId==0))){

							uni.redirectTo({
								url:'/pages/activeB/result/index'
							})
							return;
						}else if(!this.toPage&&this.mbtiStatus == 2&&(this.teamId>0||this.teamId==-1)){
							if(this.userStage=='taskConfirm'){
								uni.redirectTo({
									url:'/pages/activeB/teamList/index'
								})
							}else{
								uni.redirectTo({
									url:'/pages/activeB/teamCenter/index'
								})
							}
							return;
						}
					}

					if(this.toPage){
						uni.redirectTo({
							url:this.toPage
						})
						return false;
					}
					if(this.activity=='activeA'){
						let hasComing = uni.getStorageSync('haswm');
						let redirectUrl;
						if(hasComing&&user.nickname&&user.avatar){
							redirectUrl = '/pages/activeA/self/self'
						}else{
							redirectUrl = '/pages/activeA/index/index'
						}
						uni.redirectTo({
							url:redirectUrl
						})
					}
					if(this.activity=='activeB'){
						uni.redirectTo({
							url:'/pages/activeB/index/index'
						})
					}
					// this.noPermission();
			},
			loadData(){
				return http.initData().then(res=>{
					this.mbtiStatus = res.data.mbtiUser.mbtiStatus
					this.teamId = res.data.mbtiUser.teamId;
					this.userStage = res.data.mbtiUser.userStage
				})
			},
			/*显示无访问权限
			*/
			noPermission(identity){
				uni.redirectTo({
					url:`/pages/permission?identity=${identity}`
				})
			},
			async loadGlobalData(){
				/* #ifdef MP */
				// await this.getOssConfig();
				/* #endif */
				/*********************************************/
				if(this.activity=='activeA'){

				}
				if(this.activity=='activeB'){

				}
			},
			getOssConfig(){
				return http.ossConfig().then(res=>{
					publicData.ossConfig = res.data;
				})
			},
		}
	}
</script>

<style lang="scss">
.iPage{

	background:#369eff;
}
.loadbg{
	position: relative;
	width:100%;
	z-index:2;
}
.outer{
	position:absolute;
	width:415rpx;
	height:40rpx;
	left:5%;
	top:39%;
	border:2rpx solid #000;
	border-radius:20rpx;
	background-color:#fff;
	overflow: hidden;
	.inner{
		position:absolute;
		left:0;
		top:0;
		height:100%;

		border-radius:20rpx;
		// border:2rpx solid #000;
		background-color:#f1ee41;
		.earth{
			position: absolute;
			width:45rpx;
			bottom:100%;
			right:-14rpx;
			&.earth1{
				height:56rpx;
			}
		}
	}
	&.outnone{
		border:none;
		background-color:transparent;
		overflow: visible;
		.inner{
			background-color:transparent;
		}
	}
	&.out2{
		width:464rpx;
		left:10%;
		height:32rpx;
		top:30%;
		.inner .earth{
			width:50rpx;
			bottom:-66%;
			right: -20rpx;
		}
	}
}
</style>
